package org.example;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.dao.mapper.CarMapper;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

/**
 * MyBatis入门程序
 * @author 老杜
 * @since 1.0
 * @version 1.0
 */
public class MyBatisIntroductionTest {
    public static void main(String[] args) throws IOException {
        // 1. 创建SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        // 2. 创建SqlSessionFactory对象
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
        // 3. 创建SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 4. 执行sql
//        int count = sqlSession.insert("insertCar"); // 这个"insertCar"必须是sql的id
        HashMap<String, Object> map = new HashMap<>();
        map.put("car_num", "111");
        map.put("brand", "111");
        map.put("guide_price", "111");
        map.put("produce_time", "111");
        map.put("car_type", "111");
        int count = sqlSession.insert("insertCar", map);
        System.out.println("插入几条数据：" + count);
        // 5. 提交（mybatis默认采用的事务管理器是JDBC，默认是不提交的，需要手动提交。）
//        sqlSession.commit();
        // 6. 关闭资源（只关闭是不会提交的）
        sqlSession.close();

        CarMapper carMapper = sqlSession.getMapper(CarMapper.class);
        carMapper.insert(null);
    }


    private void test2() {

    }

    private void test3() {

    }

}